package es.nadalgoyo.frikiquest.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

import android.app.Activity;
import android.content.Context;



public class FtpCliente {

	private Activity act;

	private FTPClient client;
	
	private boolean conected = true;

	public boolean isConected() {
		return conected;
	}

	public Activity getAct() {
		return act;
	}

	public String ficheroExcel;

	public static final String FICHERO_PROPERTIES = "excel.properties";

	public static final String DIR_FICHERO_PROPERTIES = "/propiedades/";

	public FtpCliente(String host, int port, String user, String pass,
			Activity act) {
		super();
		this.act = act;

		client = new FTPClient();

		try {
			client.connect(host, port);
			client.login(user, pass);
			client.enterLocalPassiveMode();
		} catch (SocketException e) {
			e.printStackTrace();
			conected = false;
		} catch (IOException e) {
			e.printStackTrace();
			conected = false;
		}

	}

	public String getFicheroExcel() {
		return ficheroExcel;
	}

	private InputStream getInputStream(String path, String strFile) {
		InputStream is = null;

		try {

			FileOutputStream dfile = act.openFileOutput(strFile,
					Context.MODE_PRIVATE);
			client.retrieveFile(path.concat(strFile), dfile);

			is = act.openFileInput(strFile);

		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return is;
	}

	public InputStream getExcelInputStream(String pathFichero,
			String nombreFichero) {
		ficheroExcel = nombreFichero;
		return getInputStream(pathFichero, nombreFichero);
	}

	public void downloadVersionFile() {

		try {
			FileOutputStream dfile = act.openFileOutput(FICHERO_PROPERTIES,
					Context.MODE_PRIVATE);
			client.retrieveFile(DIR_FICHERO_PROPERTIES + FICHERO_PROPERTIES,
					dfile);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public byte[] getImagen(String dir, String strFile) throws SocketException, IOException {
		FileOutputStream dfile = null;
		dfile = act.openFileOutput(strFile, Context.MODE_PRIVATE);
		client.retrieveFile(dir.concat(strFile), dfile);

		File file = getLocalFile(strFile);

		InputStream is = new FileInputStream(file);

		byte[] bytes = new byte[(int) file.length()];

		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}
		if (offset < bytes.length) {
			throw new IOException(
					"No se ha podido leer completamiente el fichero "
							+ file.getName());
		}

		// Close the input stream and return bytes
		is.close();
		return bytes;
	}

	public void close() {
		if (client.isConnected())
			try {
				client.logout();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public File getExcelLocalFile() {
		return new File(getAct().getApplication().getFilesDir()
				.getAbsolutePath().concat(File.separator).concat(ficheroExcel));
	}

	public File getPropertiesLocalFile() {
		return getLocalFile(FtpCliente.FICHERO_PROPERTIES);
	}

	public File getLocalFile(String fileName) {
		return new File(getAct().getApplication().getFilesDir()
				.getAbsolutePath().concat(File.separator).concat(fileName));
	}
}
